package anthology.daily_question._24_10;
//https://leetcode.cn/problems/minimum-operations-to-make-binary-array-elements-equal-to-one-i/description/?envType=daily-question&envId=2024-10-18
public class _3191使二进制数组全部等于1的最少操作次数1 {
    static public int minOperations(int[] nums) {
        int len = nums.length;
        int count = 0;
        for(int i=0; i<len; i++){
            if(nums[i] == 0){
                if(i > len-3)
                    return -1;
                nums[i] ^=1;
                nums[i+1] ^=1;
                nums[i+2] ^=1;
                count++;
            }
        }
        return count;
    }
    static public int minOperations2(int[] nums) {
        int n = nums.length;
        int ans = 0;
        for (int i = 0; i < n - 2; i++) {
            if (nums[i] == 0) { // 必须操作
                nums[i + 1] ^= 1;
                nums[i + 2] ^= 1;
                ans++;
            }
        }
        return nums[n - 2] != 0 && nums[n - 1] != 0 ? ans : -1;
    }
    public static void main(String[] args) {
        int[] a = {0,1,1,1,0,0};
        System.out.println(minOperations(a));
    }

}
